package com.maybach.core.dubbo.consumer.adapter;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.audi.dubbo.api.brand.BrandService;
import com.audi.dubbo.api.brand.vos.BrandVo;
import com.audi.dubbo.api.brand.vos.ModelVo;
import com.audi.dubbo.api.brand.vos.SeriesVo;
import com.audi.dubbo.api.common.Result;
import com.maybach.common.enums.BaseConstants;
import com.maybach.core.dubbo.consumer.annotation.Monitor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by songshuang on 16/3/2.
 */
@Component
@Monitor(serviceName = "brandService",desc = "品牌服务")
public class BrandServiceDubboAdapter {

    private static final Logger log = LoggerFactory.getLogger(BrandServiceDubboAdapter.class);

    @Autowired
    private BrandService brandService;

    /**
     * 获取所有品牌信息
     * @return
     */
    public Result<List<BrandVo>> getAllBrand() {

        Result<List<BrandVo>> result = new Result<>();

        try {
            List<BrandVo> list = brandService.getAllBrand();
            if (list == null || list.size() == 0) {
                result.setSuccess(false);
                result.setCode(String.valueOf(BaseConstants.ResultCode.NO_DATA));
                result.setMessage(BaseConstants.getMessage(BaseConstants.ResultCode.NO_DATA));
            } else {
                result.setData(list);
                result.setSuccess(true);
            }
        } catch (Exception e) {
            log.error("获取所有品牌失败: " + BaseConstants.getMessage(BaseConstants.ResultCode.NO_DATA));
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 通过品牌ID获得车系信息
     * @param brandId
     * @return
     */
    public Result<List<SeriesVo>> getSeriesByBrandId(Long brandId) {
        Result<List<SeriesVo>> result = new Result<>();
        try {
//            List<SeriesVo> list = brandService.getSeriesByBrandId(brandId);
            List<SeriesVo> list = brandService.getAllSeriesByBrandId(brandId);
            if (list == null || list.size() == 0) {
                log.warn("根据品牌获取系列为空[brandId="+brandId+"]");
                result.setSuccess(false);
                result.setCode(String.valueOf(BaseConstants.ResultCode.NO_DATA));
                result.setMessage(BaseConstants.getMessage(BaseConstants.ResultCode.NO_DATA));
            } else {
                result.setData(list);
                result.setSuccess(true);
            }
        } catch (Exception e) {
            log.error("通过品牌ID获得车系信息失败: [brandId = " + brandId + "]", e);
            //e.printStackTrace();
        }

        return result;
    }

    /**
     * 通过车系ID获取所有车系信息
     * @param seriesId
     * @return
     */
    public Result<List<ModelVo>> getModelsBySeriesId(Long seriesId) {
        Result<List<ModelVo>> result = new Result<>();
        try {
            List<ModelVo> list = brandService.getModelsBySeriesId(seriesId);
            if (list == null || list.size() == 0) {
                result.setSuccess(false);
                result.setCode(String.valueOf(BaseConstants.ResultCode.NO_DATA));
                result.setMessage(BaseConstants.getMessage(BaseConstants.ResultCode.NO_DATA));
            } else {
                result.setData(list);
                result.setSuccess(true);
            }
        } catch (Exception e) {
            log.error("通过车系ID获取车型信息[seriesId = " + seriesId + "]");
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 通过品牌ID列表获取指定的品牌
     * @param ids
     * @return
     */
    public Result<List<BrandVo>> getBrandListByIds(List<Long> ids){
        Result<List<BrandVo>> result = new Result<>();
        try {
            List<BrandVo> list = brandService.getBrandListByIds(ids);
            if (list == null || list.size() == 0) {
                result.setSuccess(false);
                result.setCode(String.valueOf(BaseConstants.ResultCode.NO_DATA));
                result.setMessage(BaseConstants.getMessage(BaseConstants.ResultCode.NO_DATA));
            } else {
                result.setData(list);
                result.setSuccess(true);
            }
        } catch (Exception e) {
            log.error("通过品牌ID列表获取指定的品牌失败: [ids = " + ids +"]");
            e.printStackTrace();
        }

        return result;
    }
}
